<?php defined('BASEPATH') or exit('No direct script access allowed');

class Home_m extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->load->helper('file');
	}

/**	
	function changeCountry_async($country_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		$country_id = intval($country_id);
		
		$COUNTRY = $this->countries_entity->init('id',$country_id);
		//$stateArray = getStateData_ioc($country_id);
		//$mixLocation = getMixLocationEscorts($country_id);
		
		//$html = form_dropdown('state_province_city_id', $mixLocation, array() , 'id="state_id_sys"' );
		$html = "<select id='state_id_sys' class='custom' name='state_province_city_id' >";
		
		$states = $this->db->query("SELECT DISTINCT state_id FROM ".TBL_ESCORT." WHERE country_id=$country_id")->result();
		foreach($states as $item){
			$state_id = $item->state_id;
			$stateRes = getStateData_ioc($country_id,$state_id);
			$tmp = array_values($stateRes);
			$html .= "<option value='$state_id'>".$tmp[0]."</option>";
			
			if($COUNTRY->getCountryCode() == 'ES'){
				$provinces = $this->db->query("SELECT DISTINCT province_id FROM ".TBL_ESCORT." WHERE state_id=$state_id")->result();
				foreach($provinces as $item2 ){
					$province_id = $item2->province_id;
					$provinceArr = getProvinceData_ioc($country_id,$state_id,$province_id);
					$tmp1 = array_values($provinceArr);
					
					$html .= "<option value='$state_id|$province_id'>&nbsp;&nbsp;".$tmp1[0]."</option>";
					$cities = $this->db->query("SELECT DISTINCT city_id FROM ".TBL_ESCORT." WHERE province_id=$province_id")->result();
					foreach($cities as $item3){
						$city_id = $item3->city_id;
						$cityArr = getCityData_ioc($country_id,$state_id,$province_id,$city_id);
						$tmp2 = array_values($cityArr);
						
						$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
					}
				}
			}else{
				$cities = $this->db->query("SELECT DISTINCT city_id FROM ".TBL_ESCORT." WHERE state_id=$state_id")->result();
				$province_id = 0;
				foreach($cities as $item3){
					$city_id = $item3->city_id;
					$cityArr = getCityData_ioc($country_id,$state_id,$province_id,$city_id);
					$tmp2 = array_values($cityArr);
					
					$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
				}
			}
		}
		$html .= "</select>";
		return $html;
	}
**/
	function findLocationInfo(){
		$country_id = isset($_GET['country_id'])? intval($_GET['country_id']):0;
		if(!isset($_GET['state_province_city_id']) OR $_GET['state_province_city_id'] == -1 OR $_GET['state_province_city_id'] == 0){
			if(isset($_GET['state_id'])){
				$sys_state_id = intval($_GET['state_id']);
			}else{
				$sys_state_id = 0;
			}
			
			if(isset($_GET['province_id'])){
				$sys_province_id = intval($_GET['province_id']);
			}else{
				$sys_province_id = 0;
			}
			
			if(isset($_GET['city_id'])){
				$sys_city_id = intval($_GET['city_id']);
			}else{
				$sys_city_id = -1;
			}
		}else{
			$exp = explode('|',$_GET['state_province_city_id']);
			$exp[0] = isset($exp[0]) ? intval($exp[0]):0;
			$exp[1] = isset($exp[1]) ? intval($exp[1]):0;
			$exp[2] = isset($exp[2]) ? intval($exp[2]):0;
			
			$sys_state_id = ($exp[0] >0 ) ? $exp[0]:0;
			$sys_city_id  = ($exp[2]>0) ? $exp[2]:0;
			$sys_province_id = ($exp[1]) ? $exp[1]:0;
		}
		
		$segment = $this->uri->segment(2,'');
		$fixedVar = false;
		if($segment){
			$linkArr = reDirectSite();
			if( in_array($segment,array_keys($linkArr)) ){
				$fixedVar = true;
			}
		}
		 
		if($fixedVar){
			$country_id = $linkArr[$segment]['country_id'];
			$sys_state_id = $linkArr[$segment]['state_id'];
			$sys_province_id = $linkArr[$segment]['province_id'];
			$sys_city_id = $linkArr[$segment]['city_id'];
		}
		
		$country_arr = $state_arr = $province_arr = $city_arr = array();
		
		if($country_id != 0){
			$country_arr = getCountryData_ioc( $country_id );
		}
		
		if($sys_state_id != 0){
			$state_arr = getStateData_ioc( $country_id, $sys_state_id );
		}
		
		if($sys_province_id != 0){
			$province_arr = getProvinceData_ioc( $country_id, $sys_state_id, $sys_province_id );
		}
		
		if($sys_city_id != 0){
			$city_arr = getCityData_ioc( $country_id, $sys_state_id, $sys_province_id, $sys_city_id );
		}
		
		$countryname = $statename= $provincename = $cityname = ''; 
		if( $city_arr){
			$cityname = getValueOfArray($city_arr);
		}
		if($province_arr){
			$provincename = getValueOfArray($province_arr);
		}
		if($state_arr){
			$statename = getValueOfArray($state_arr);
		}
		if($country_arr){
			$countryname = getValueOfArray($country_arr);
		}
		return array(
			'country'	=> $countryname,
			'state'		=> $statename,
			'province'	=> $provincename,
			'city'		=> $cityname,
			'country_id'	=> $country_id,
			'state_id'		=> $sys_state_id,
			'province_id'	=> $sys_province_id,
			'city_id'		=> $sys_city_id
		);
	}
	
	function listingPlaceLocation() {
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$html = "<select id='place_listing' class='normal' name='place_listing' onchange='return loadListingPlaceElement(this.value);'>";
		$rc = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." GROUP BY country_id,state_id,province_id,city_id")->result();
		
		foreach($rc as $item){
			$placeInfo = getPlaceListDataInfoById($item->id);
			$country = $placeInfo['country'];
			$state = ($placeInfo['state']) ? ' / '.$placeInfo['state']:'';
			$province = ($placeInfo['province']) ? ' / '.$placeInfo['province']:'';
			$city = ($placeInfo['city']) ? ' / '.$placeInfo['city']:'';
			
			$html .= "<option value='{$item->id}'>".$country.$state.$province.$city.' '.$placeInfo['price'].' &euro;'."</option>";
		}
		$html .= "</select>";
		$this->db->set_dbprefix($current_dbprefix);
		
		return $html;
	}
	
	function changeCountry_async($country_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$TBL_SELECT = 'default_escort_site_settings_on_zones';
		
		$COUNTRY = $this->countries_entity->init('id',$country_id);
		$html = "<select id='state_id_sys' class='custom' name='state_province_city_id' >";
		
		$cityData = getCityInfoFromUserGeoLocation();
		
		if(isset($_GET['country_id'])){
			$country_id = intval($_GET['country_id']);
		}else{
			$country_id = intval($country_id);
		}
		
		if(!isset($_GET['state_province_city_id']) OR $_GET['state_province_city_id'] == -1 OR $_GET['state_province_city_id'] == 0){
			if(isset($_GET['state_id'])){
				$sys_state_id = intval($_GET['state_id']);
			}else{
				$sys_state_id = -1;
			}
			
			if(isset($_GET['city_id'])){
				$sys_city_id = intval($_GET['city_id']);
			}else{
				$sys_city_id = -1;
			}
			
			if(isset($_GET['province_id'])){
				$sys_province_id = intval($_GET['province_id']);
			}else{
				$sys_province_id = -1;
			}
			
		}else{
			$exp = explode('|',$_GET['state_province_city_id']);
			$exp[0] = isset($exp[0]) ? intval($exp[0]):-1;
			$exp[1] = isset($exp[1]) ? intval($exp[1]):-1;
			$exp[2] = isset($exp[2]) ? intval($exp[2]):-1;
			
			$sys_state_id = ($exp[0] >0 ) ? $exp[0]:-1;
			$sys_city_id  = ($exp[2]>0) ? $exp[2]:-1;
			$sys_province_id = ($exp[1]>0) ? $exp[1]:-1;
		}
		
		if($COUNTRY){
			$html .= "<option value='-1'>".language_translate('selection_option_all')."</option>";
			$states = $this->db->query("SELECT DISTINCT region_id FROM $TBL_SELECT WHERE country_id=$country_id AND site_id=".get_site_id())->result();
			foreach($states as $item){
				$state_id = $item->region_id;
				$stateRes = getStateData_ioc($country_id,$state_id);
				$tmp = array_values($stateRes);
				
				if($state_id > 0){
					if($state_id == $sys_state_id ){
						$html .= "<option value='$state_id' STATE_SELECT>".$tmp[0]."</option>";
					}else{
						if( $sys_state_id == -1 ){
							if( strtoupper( slugify($tmp[0]) ) == strtoupper( slugify($cityData['region_name']) ) ){
								$html .= "<option value='$state_id' STATE_SELECT>".$tmp[0]."</option>";
							}else{
								$html .= "<option value='$state_id'>".$tmp[0]."</option>";
							}
						}else{
							$html .= "<option value='$state_id'>".$tmp[0]."</option>";
						}
					}
				}
				
				if($COUNTRY->getCountryCode() == 'ES'){
					$provinces = $this->db->query("SELECT DISTINCT province_id FROM $TBL_SELECT WHERE region_id=$state_id AND site_id=".get_site_id())->result();
					foreach($provinces as $item2 ){
						$province_id = $item2->province_id;
						$provinceArr = getProvinceData_ioc($country_id,$state_id,$province_id);
						$tmp1 = array_values($provinceArr);
						
						if($province_id > 0){
							if($sys_province_id == $province_id){
								$html .= "<option value='$state_id|$province_id' PROVINCE_SELECT>&nbsp;&nbsp;".$tmp1[0]."</option>";
							}else{
								$html .= "<option value='$state_id|$province_id'>&nbsp;&nbsp;".$tmp1[0]."</option>";
							}
						}
						
						$cities = $this->db->query("SELECT DISTINCT city_id FROM $TBL_SELECT WHERE province_id=$province_id AND site_id=".get_site_id())->result();
						foreach($cities as $item3){
							$city_id = $item3->city_id;
							$cityArr = getCityData_ioc($country_id,$state_id,$province_id,$city_id);
							$tmp2 = array_values($cityArr);
							
							if($city_id > 0){
								if($city_id == $sys_city_id){
									$html .= "<option value='$state_id|$province_id|$city_id' CITY_SELECT>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
								}else{
									if($sys_city_id == -1){
										if( strtoupper( slugify($tmp2[0]) ) == strtoupper( slugify($cityData['city_name']) ) ){
											if($tmp2[0] != '' AND $tmp2[0] != '.'){
												$html .= "<option value='$state_id|$province_id|$city_id' CITY_SELECT>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
											}
										}else{
											if($tmp2[0] != '' AND $tmp2[0] != '.'){
												$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
											}
										}
									}else{
										if($tmp2[0] != '' AND $tmp2[0] != '.'){
											$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
										}
									}
								}
							}
						}
					}
				}else{
					$cities = $this->db->query("SELECT DISTINCT city_id FROM $TBL_SELECT WHERE region_id=$state_id AND site_id=".get_site_id())->result();
					$province_id = 0;
					foreach($cities as $item3){
						$city_id = $item3->city_id;
						$cityArr = getCityData_ioc($country_id,$state_id,$province_id,$city_id);
						$tmp2 = array_values($cityArr);
						
						if($city_id > 0){
							if($city_id == $sys_city_id){
								$html .= "<option value='$state_id|$province_id|$city_id' CITY_SELECT>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
							}else{
								if($sys_city_id == -1){
									if( strtoupper( slugify($tmp2[0]) ) == strtoupper( slugify($cityData['city_name']) ) ){
										if($tmp2[0] != '' AND $tmp2[0] != '.'){
											$html .= "<option value='$state_id|$province_id|$city_id' CITY_SELECT>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
										}
									}else{
										if($tmp2[0] != '' AND $tmp2[0] != '.'){
											$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
										}
									}
								}else{
									if($tmp2[0] != '' AND $tmp2[0] != '.'){
										$html .= "<option value='$state_id|$province_id|$city_id'>&nbsp;&nbsp;&nbsp;&nbsp;".$tmp2[0]."</option>";
									}
								}
							}	
						}
					}
				}
			}
		}
		$html .= "</select>";
		
		
		if(strpos($html, 'CITY_SELECT') !== false AND strpos($html, 'STATE_SELECT') !== false AND strpos($html, 'PROVINCE_SELECT') !== false){
			$html = str_replace(array('CITY_SELECT','STATE_SELECT','PROVINCE_SELECT'), array('selected="selected"','',''), $html);
		}else if(strpos($html, 'CITY_SELECT') == false AND strpos($html, 'STATE_SELECT') !== false AND strpos($html, 'PROVINCE_SELECT') !== false){
			$html = str_replace(array('STATE_SELECT','PROVINCE_SELECT'), array('','selected="selected"'), $html);
		}else if(strpos($html, 'CITY_SELECT') == false AND strpos($html, 'STATE_SELECT') == false AND strpos($html, 'PROVINCE_SELECT') !== false){
			$html = str_replace(array('PROVINCE_SELECT'), array('selected="selected"'), $html);
		}else if(strpos($html, 'CITY_SELECT') != false AND strpos($html, 'STATE_SELECT') != false AND strpos($html, 'PROVINCE_SELECT') == false){
			$html = str_replace(array('STATE_SELECT','CITY_SELECT'), array('','selected="selected"'), $html);
		}else if(strpos($html, 'CITY_SELECT') == false AND strpos($html, 'STATE_SELECT') != false AND strpos($html, 'PROVINCE_SELECT') == false){
			$html = str_replace(array('STATE_SELECT'), array('selected="selected"'), $html);
		}
		
		
		$this->db->set_dbprefix($current_dbprefix);
		return $html;
	}
		
	function changeState_async(){
		$state_id = $this->input->post('state_id',0);
		$country_id = $this->input->post('country_id',0);
		$provinceArray = getProvinceData_ioc($country_id,$state_id);
		 
		$html = form_dropdown('province_id', $provinceArray, array() , 'id="province_id_sys"' );
		echo $html;
	}
	
	function changeProvince_async(){
		$state_id = $this->input->post('state_id',0);
		$province_id = $this->input->post('province_id',0);
		$country_id = $this->input->post('country_id',0);
		$cityArray = getCityData_ioc($country_id,$state_id,$province_id);
		
		$html = form_dropdown('city_id', $cityArray, array() , 'id="city_id_sys"' );
		echo $html;
	}
	 
	function updateNumberClicked(){
		$media_id = intval($this->input->post('media_id',TRUE));
		$MEDIA = $this->media_entity->init('id',$media_id);
		if($MEDIA){
			$MEDIA->setClickedNo( $MEDIA->getClickedNo() +1 );
			$MEDIA->update();
		}		
	} 
	
	function updateNumberSeeMobilePhone(){
		$escort_id = intval($this->input->post('escort_id',TRUE));
		$ESCORT = $this->escort_entity->init('id',$escort_id);
		
		$ESCORT->setTimesPhoneDisplayed( 1 + $ESCORT->getTimesPhoneDisplayed() );
		$ESCORT->update();
	}
	
	function bannerclicked(){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		$bannerid = intval($this->input->post('id',TRUE));
		
		$rs = $this->db->where('ads_banner_id',$bannerid)->get(TBL_BANNER_STATISTIC)->result();
		$rs2 = $this->db->where('id',$bannerid)->get(TBL_BANNER)->result();
		
		$ipAdd			= $this->session->userdata('ip_address');
		$this->db->set('ads_banner_id',$bannerid);
		$this->db->set('site_ref',SITE_REF);
		$this->db->set('page_url',$rs2[0]->link);
		$this->db->set('ip',$ipAdd);
		$this->db->set('time',time());
		
		if($rs){
			$this->db->set('clicked',1+$rs[0]->clicked);
			$this->db->where('id', $rs[0]->id)->update(TBL_BANNER_STATISTIC);
		}else{
			$this->db->set('clicked',1);
			$this->db->insert(TBL_BANNER_STATISTIC);
		}
		echo $rs2[0]->link;
		$this->db->set_dbprefix($current_dbprefix);
	}
	
	function contactus(){
		extract($this->input->post());
		
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$captcha = strtolower($captcha);
		
		if( $correctCaptcha != $captcha){
			echo json_encode( array('result'=>'err','msg'=>language_translate('authorization_m_captcha_err1')) );
			exit();
		}
	 	if( time() > $expTime ){
			echo json_encode( array('result'=>'err','msg'=>language_translate('authorization_m_captcha_err2')) );
			exit();
		}
		if(strlen($name) == 0){
			echo json_encode( array('result'=>'err','msg'=>language_translate('contact_sys_name_error')) );
			exit();			
		}
		if(!$this->phpvalidator->is_email($email)){
			echo json_encode( array('result'=>'err','msg'=>language_translate('contact_sys_email_error')) );
			exit();			
		}
		if(strlen($subject) == 0){
			echo json_encode( array('result'=>'err','msg'=>language_translate('contact_sys_subject_error')) );
			exit();			
		}
		if(strlen($message) == 0){
			echo json_encode( array('result'=>'err','msg'=>language_translate('contact_sys_message_error')) );
			exit();			
		}
		
		$name = substr($name,0,45);
		$subject= substr($subject,0,145 );
		$message = substr($message,0,550);
		
		$header = language_translate('contact_sys_label_from').": $email <br/>".
				language_translate('contact_sys_label_contact_name').": $name <br/>". 
				language_translate('contact_sys_label_mobile').": $mobile <br/>".
				language_translate('contact_sys_label_subject').": $subject <br/>";
		
		 	
		$contactServerEmail = read_file('./config/contact_email.txt');
			
		$this->email_sender->setFromEmail($email);
		$this->email_sender->setToEmail($contactServerEmail);
		$this->email_sender->setSubject($subject);
		$this->email_sender->setBody($header.'<br/>'.$message);
		$this->email_sender->setEmailTitle($email);
		
		$this->email_sender->sendEmail();
		
		echo json_encode( array('result'=>'ok','msg'=>language_translate('contact_form_message')) );//Thanks for your contact.
		exit();
	}
	
	function report_abuse(){
		$link = $this->input->post('link','');
		$slug = 'report_abuse_escort_email';
		
		$template = $this->module_helper->getTemplateMail( $slug );
		$body = $template['body'];
		$subject = $template['subject'];
		
		$strLink = "<a href='$link'>".$link."</a>";
		
		$body = str_replace(array('{###LINK###}'),
							 array($strLink), $body);

		$reportAbuseEmail = read_file('./config/report_abuse_email.txt');
							 
		$this->email_sender->setToEmail($reportAbuseEmail);
		$this->email_sender->setSubject($subject);
		$this->email_sender->setBody($body);
		$this->email_sender->sendEmail();
		echo json_encode(array('result'=>'ok','msg'=>language_translate('myzone_page_escort_report_abuse_OK')));
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	function crontab_services(){
		$rc = $this->payment_log_entity->getContractTimeExceed();
		foreach($rc as $item){
			$PAY_LOG = $this->payment_log_entity->init('id',$item->id);
			if($PAY_LOG->getUserType() == 'escort'){
				$ESCORT = $this->escort_entity->init('id',$PAY_LOG->getEscortId());
				
				$ESCORT->setSubscriptionId(0);
				$ESCORT->setPublished(0);
				$ESCORT->setPlacesListIdsContract('');
				$ESCORT->setPackageIds('');
				$ESCORT->setPriorityId('');
				$ESCORT->update();
				
				/**
				if($PAY_LOG->getServiceType() == 'SUBS'){
					$ESCORT->setSubscriptionId(0);
					$ESCORT->setPublished(0);
					$ESCORT->update();
				}
				if($PAY_LOG->getServiceType() == 'PLACE'){
					$myPlaceListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PLACE');
					$ESCORT->setPlacesListIdsContract($myPlaceListArray);
					$ESCORT->update();
				}
				if($PAY_LOG->getServiceType() == 'PACK'){
					$myPackListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PACK');
					$ESCORT->setPackageIds($myPackListArray);
					$ESCORT->update();
				}
				if($PAY_LOG->getServiceType() == 'PRI'){
					$myPriorityListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PRI');
					$ESCORT->setPriorityId($myPriorityListArray);
					$ESCORT->update();
				}
				**/
			}else{
				$escorts = $this->db->where( array('user_id'=>$PAY_LOG->getUserId(),'apply_services'=>1) )->get(TBL_ESCORT)->result();
				foreach($escorts as $item){
					$ESCORT = $this->escort_entity->init('id',$item->id);
					
					$ESCORT->setSubscriptionId(0);
					$ESCORT->setPublished(0);
					$ESCORT->setPlacesListIdsContract('');
					$ESCORT->setPackageIds('');
					$ESCORT->setPriorityId('');
					$ESCORT->update();
					
					/**
					if($PAY_LOG->getServiceType() == 'SUBS'){
						$ESCORT->setSubscriptionId(0);
						$ESCORT->setPublished(0);
						$ESCORT->update();
					}
					if($PAY_LOG->getServiceType() == 'PLACE'){
						$myPlaceListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PLACE');
						$ESCORT->setPlacesListIdsContract($myPlaceListArray);
						$ESCORT->update();
					}
					if($PAY_LOG->getServiceType() == 'PACK'){
						$myPackListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PACK');
						$ESCORT->setPackageIds($myPackListArray);
						$ESCORT->update();
					}
					if($PAY_LOG->getServiceType() == 'PRI'){
						$myPriorityListArray = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PRI');
						$ESCORT->setPriorityId($myPriorityListArray);
						$ESCORT->update();
					}
					**/
				}
			}
			$PAY_LOG->setPaymentStatus('pending');
			$PAY_LOG->update();
		}
	}
	
//end class	 
}